package com.risk.modules.rm.dao;

import com.risk.modules.rm.entity.RuleList;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface RuleListDao {
    @Insert(value="INSERT INTO `rule_list`(`id`, `name`, `list_no`, `memo`, `rule_list`,`user_id`,`is_default`, `create_by`, `create_time`, `update_by`, `update_time`) " +
            "VALUES (#{ruleList.id}, #{ruleList.name}, #{ruleList.listNo}, #{ruleList.memo}, #{ruleList.ruleList},#{ruleList.userId},#{ruleList.isDefault}, #{ruleList.createBy}, #{ruleList.createTime}, #{ruleList.updateBy},#{ruleList.updateTime})")
    int add(@Param("ruleList") RuleList ruleList);

    @Select(value = "SELECT * FROM `rule_list` WHERE id = #{0} and del_flag = 0")
    RuleList get(String id);

    @Select(value = "SELECT * FROM `rule_list` WHERE 1=1 and del_flag = 0 ${whereClause}  ORDER BY create_time DESC LIMIT #{0},#{1} ")
    List<RuleList> pageByCondition(int start, int size, @Param("whereClause") String whereClause);

    @Select(value = "SELECT count(1) FROM `rule_list` WHERE 1=1 and del_flag = 0 ${whereClause}")
    int countByCondition(@Param("whereClause") String whereClause);

    @Update(value = "UPDATE `rule_list` SET `del_flag` = '1' WHERE `id` = #{0} and `user_id` = #{1}")
    int del(String id, Integer userId);

    @Update(value = "UPDATE `rule_list` SET `name` = #{ruleList.name}, `list_no` = #{ruleList.listNo}, `memo` = #{ruleList.memo}, #{ruleList.ruleList}, " +
            "`is_default` = #{ruleList.isDefault}, `update_time` = #{ruleList.updateTime} WHERE `id` = #{ruleList.userId}")
    int update(@Param("ruleList") RuleList ruleList);

    @Update(value = "UPDATE `rule_list` SET `is_default` = '0' WHERE `id` != #{0} and `user_id` = #{1}")
    int updateDefault(String id, Integer userId);

    @Update(value = "UPDATE `rule_list` SET `is_default` = '1' WHERE `id` = #{0} and `user_id` = #{1}")
    int setDefault(String id, Integer userId);
}
